Skip to content

gh-146527: Fix memory leak in _PyGC_Fini()#150969

Merged
vstinner merged 2 commits into
python:mainfrom
vstinner:free_gc
Jun 5, 2026
Merged

gh-146527: Fix memory leak in _PyGC_Fini()#150969
vstinner merged 2 commits into
python:mainfrom
vstinner:free_gc

Conversation

@vstinner

@vstinner vstinner commented Jun 5, 2026

Copy link
Copy Markdown
Member

Free generation_stats allocated by _PyGC_Init().

Fix Python/gc.c: Python/gc_free_threading.c was already fixed.

Free generation_stats allocated by _PyGC_Init().

Fix Python/gc.c: Python/gc_free_threading.c was already fixed.
@vstinner vstinner added the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 5, 2026
@vstinner

vstinner commented Jun 5, 2026

Copy link
Copy Markdown
Member Author

For example, the leak can be seen with Valgrind.

$ PYTHONMALLOC=malloc valgrind --leak-check=full --show-leak-kinds=all --num-callers=20 ./python -c pass
(...)
==3201846== HEAP SUMMARY:
==3201846==     in use at exit: 1,112 bytes in 1 blocks
==3201846==   total heap usage: 28,765 allocs, 28,764 frees, 6,620,982 bytes allocated
==3201846== 
==3201846== 1,112 bytes in 1 blocks are still reachable in loss record 1 of 1
==3201846==    at 0x484862B: calloc (vg_replace_malloc.c:1616)
==3201846==    by 0x4FE115: _PyMem_RawCalloc (obmalloc.c:80)
==3201846==    by 0x511FC1: PyMem_RawCalloc (obmalloc.c:1222)
==3201846==    by 0x62A744: _PyGC_Init (gc.c:137)
==3201846==    by 0x65DB65: pycore_interp_init (pylifecycle.c:975)
==3201846==    by 0x65DD71: pyinit_config (pylifecycle.c:1040)
==3201846==    by 0x666D3A: pyinit_core (pylifecycle.c:1203)
==3201846==    by 0x666E13: Py_InitializeFromConfig (pylifecycle.c:1631)
==3201846==    by 0x69888E: pymain_init (main.c:69)
==3201846==    by 0x698953: pymain_main (main.c:817)
==3201846==    by 0x6989E4: Py_BytesMain (main.c:850)
==3201846==    by 0x401CEE: main (python.c:15)
==3201846== 
==3201846== LEAK SUMMARY:
==3201846==    definitely lost: 0 bytes in 0 blocks
==3201846==    indirectly lost: 0 bytes in 0 blocks
==3201846==      possibly lost: 0 bytes in 0 blocks
==3201846==    still reachable: 1,112 bytes in 1 blocks
==3201846==         suppressed: 0 bytes in 0 blocks
(...)

I found the leak when building Python with ASAN.

@vstinner vstinner enabled auto-merge (squash) June 5, 2026 13:13
@pablogsal

Copy link
Copy Markdown
Member

Thanks victor!

@vstinner vstinner merged commit 0036565 into python:main Jun 5, 2026
56 checks passed
@vstinner vstinner deleted the free_gc branch June 5, 2026 13:43
@miss-islington-app

Copy link
Copy Markdown

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Jun 5, 2026

Copy link
Copy Markdown

GH-150970 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Jun 5, 2026
vstinner added a commit that referenced this pull request Jun 5, 2026
gh-146527: Fix memory leak in _PyGC_Fini() (GH-150969)

Free generation_stats allocated by _PyGC_Init().

Fix Python/gc.c: Python/gc_free_threading.c was already fixed.
(cherry picked from commit 0036565)

Co-authored-by: Victor Stinner <vstinner@python.org>
@sergey-miryanov

Copy link
Copy Markdown
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants